//--------------------------------------------------- // Purpose: Demonstrate file input/output // Author: John Gauch and Wing Ning Li //--------------------------------------------------- #include #include #include using namespace std; int main() { // Open outfile.txt to store what the user inputs ofstream dout; dout.open("outfile.txt"); // Loop reading from key board or stdin and writing to outfile.txt // also perform analysis of each character read // some key may have several character associated with it. char ch; while (cin.get(ch)){ // Check the integer value of the characer cout << "integer value of the character is " << (int)ch << endl; // The following is based on ASCII table on page 975 of the text if (ch == 9) { cout << "tab key\n"; } else if (ch == 10){ cout << "new line key\n"; } else if (ch == 13){ cout <<"carriage return key\n"; } else if (ch <= 31){ cout << "other non printable character\n"; } else if (ch == 32) { cout << "space\n"; } else if (ch >= 33 && ch <= 47){ cout << "special character !\"#$%&'()*+,-./\n"; } else if (ch >= 48 && ch <= 57){ cout << "digits 0 to 9\n"; } else if (ch >= 58 && ch <= 64){ cout << "special characters :;<+>?@\n"; } else if (ch >= 65 && ch <= 90){ cout << "upper case letters\n"; } else if (ch >= 91 && ch <= 96){ cout << "special characters [\\]^_`\n"; } else if (ch >= 97 && ch <= 122){ cout << "lower case letters\n"; } else if (ch >= 123 && ch <= 126){ cout << "special characters\n"; } else if (ch == 127){ cout << "DEL\n"; } else { cout << "not a standard ASCII character, first bit is 1\n"; } // Write the character to the output file dout << ch; } // Close output file dout.close(); // We now try to read from the file we just written ifstream din; din.open("outfile.txt"); // Assume open is ok and loop reading characters while (din.get(ch)){ cout << ch; } // Close input file din.close(); }